package com.qf.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.UUID;

/**
 * @version v1.0.0
 * @Description:
 * @author: 姜红凯
 * @date: 2022.02.14
 * @since 1.0
 */
@Controller
public class UploadController {

    @RequestMapping("/upload")
    public String upload(String username, MultipartFile file , HttpSession session){
        System.out.println("username: " + username );


        //动态获取服务器路径
        String realPath = session.getServletContext().getRealPath("/upload");
        System.out.println(realPath);

        //如果此路径不存在则创建此路径
        if(!new File(realPath).exists()){
            new File(realPath).mkdirs();
        }

        //文件的原始名称
        String originalFilename = file.getOriginalFilename();

        String uuid = UUID.randomUUID().toString();

        try {
            file.transferTo(new File(realPath+ "/" +uuid + originalFilename ));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "hello";
    }
}
